package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import Lexicon.BinaryTree;
import Lexicon.Lexicon;
import Lexicon.Word;
import Lexicon.WordType;

public class AdditionPanel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel newArWordLabel  , newEnWordLabel , newWordTypeLabel , newWordNumLabel , newWordGenLabel ;
	private JTextField arWord , enWord ;
	private JComboBox type , num ,gen ;
	String [] typeList = {ArMT.labels.getString("definedNoun"),
			              ArMT.labels.getString("indeterNoun"),
					      ArMT.labels.getString("verb")} , 
	numList = {
			ArMT.labels.getString("sing"),
			ArMT.labels.getString("dual"),
			ArMT.labels.getString("plaural")} ,
	genList = {
			ArMT.labels.getString("male"),
			ArMT.labels.getString("female")};
	JButton removeButton , addButton , returnButton ;
	JSplitPane split ;
	int typeItem , numItem , genItem ;
	static BinaryTree newDic =new BinaryTree();
	GridBagConstraints c ;
	public AdditionPanel() {
		super();
		
		setLayout(new GridBagLayout());
	    c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			
			newArWordLabel = new JLabel(ArMT.labels.getString("newArWordLabel"));
			c.gridx = 0;
			c.gridy = 0;
			c.weightx = 0.2 ;
			c.weighty = 0.2 ;
			c.ipady = 10 ;

			add(newArWordLabel , c);
			newArWordLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			arWord = new JTextField();
			c.gridx = 1;       
			c.gridwidth = 3;   
			c.gridy = 0;   
			add(arWord , c);
			arWord.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			newEnWordLabel = new JLabel(ArMT.labels.getString("newEnWordLabel"));
			c.gridx = 0;
			c.gridy = 1;
			add(newEnWordLabel , c);
			newEnWordLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			enWord = new JTextField();
			c.gridx = 1;       
			c.gridwidth = 3;   
			c.gridy = 1;   
			add(enWord , c);
			enWord.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			newWordTypeLabel = new JLabel(ArMT.labels.getString("newWordType"));
			c.gridx = 0;
			c.gridy = 2;
			add(newWordTypeLabel , c);
			newWordTypeLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			type = new JComboBox(typeList);
			c.gridx = 1;       
			c.gridwidth = 3;   
			c.gridy = 2;   
			type.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			type.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        	 	int t  = type.getSelectedIndex();
	        	 switch(t)
	        	 {
	        	 case 0 :
	        		 typeItem = WordType.DEFINED_NOUN ;
	        		 break ;
	        	 case 1 :
	        		 typeItem = WordType.INDETERMINATE_NOUN ;
	        		 break ;
	        	 case 2 :
	        		 typeItem = WordType.NORMAL_VERB ;
	        		 break ;
	        	 }
	               
	        	}
	        });	
			type.setSelectedIndex(0);
			add(type , c);
			
			
			newWordNumLabel = new JLabel(ArMT.labels.getString("newWordNumLabel"));
			c.gridx = 0;
			c.gridy = 3;
			add(newWordNumLabel , c);
			newWordNumLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			num = new JComboBox(numList);
			c.gridx = 1;       
			c.gridwidth = 3;   
			c.gridy = 3;   
			num.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			num.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        		int t  = type.getSelectedIndex();
		        	 switch(t)
		        	 {
		        	 case 0 :
		        		 numItem = WordType.SINGULAR ;
		        		 break ;
		        	 case 1 :
		        		 numItem = WordType.DUAL ;
		        		 break ;
		        	 case 2 :
		        		 numItem = WordType.PLAURAL ;
		        		 break ;
		        	 }
	                	            
	        	}
	        });		
			num.setSelectedIndex(0);
			add(num , c);
			
			
			newWordGenLabel = new JLabel(ArMT.labels.getString("newWordGenLabel"));
			c.gridx = 0;
			c.gridy = 4;
			add(newWordGenLabel,c);
			newWordGenLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			gen = new JComboBox(genList);
			c.gridx = 1;       
			c.gridwidth = 3;   
			c.gridy = 4;   
			gen.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			gen.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        		int t  = type.getSelectedIndex();
		        	 switch(t)
		        	 {
		        	 case 0 :
		        		 numItem = WordType.MALE ;
		        		 break ;
		        	 case 1 :
		        		 numItem = WordType.FEMALE ;
		        		 break ;		      
		        	 }
	                	            
	        	}
	        });
			gen.setSelectedIndex(0);
			add(gen , c);
			
			addButton = new JButton(ArMT.labels.getString("addButton")) ;
			c.gridx = 0;
			c.gridy = 5;
			c.ipadx = 0 ;
			c.ipady = 5 ;
			c.weightx = 0.1 ;
			c.weighty = 0.2 ;
			c.gridwidth = 1 ;
			addButton.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			addButton.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        		if(arWord.getText() == null)
	        			JOptionPane.showMessageDialog(null, ArMT.labels.getString("arWordEmtyMessage"));
	        		else if(enWord.getText() == null)
	        			JOptionPane.showMessageDialog(null, ArMT.labels.getString("enWordEmptyMessage"));
	        		else
	        		{
	        		newDic.insert(arWord.getText(), enWord.getText(), typeItem , numItem , genItem);
	        		Lexicon.wordTree.insert(arWord.getText(), enWord.getText(), typeItem , numItem , genItem);
	        		Word w = new Word();
					DicTable.addNewRow(arWord.getText(), enWord.getText(),w.toString(typeItem),w.toString(numItem),w.toString(genItem));
	        		}
	        	}
	        });
			add(addButton , c);
			
			removeButton = new JButton(ArMT.labels.getString("removeButton")) ;
			c.gridx = 1;
			c.gridy = 5;
			removeButton.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			
			removeButton.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        		Lexicon.wordTree.delete(DicTable.getDataAt(DicTable.getSelectedRow())) ;
	        		JOptionPane.showMessageDialog(null, DicTable.getDataAt(DicTable.getSelectedRow())) ;
	        		DicTable.removeSelectedRow(DicTable.getSelectedRow());
	        		}
	        	}
	        );
			add(removeButton , c);
			
			returnButton = new JButton(ArMT.labels.getString("cancelButton"));
			c.gridx = 2;          
			c.gridy =5;   
			returnButton.setFont(new Font("Lucida Sans", Font.BOLD, 12));
			returnButton.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent e){
	        		arWord.setText(null);
	        		enWord.setText(null);
	        		type.setSelectedIndex(0);
	        		num.setSelectedIndex(0);
	        		gen.setSelectedIndex(0);
	        	}
	        });
			add(returnButton , c);
			
			setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder(ArMT.labels.getString("newWordBorder")), BorderFactory.createEmptyBorder(5,5,5,5)
					)
					);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public JTextField getEnWord() {
		return enWord;
	}

	public JTextField getArWord() {
		return arWord;
	}
}
